from django.shortcuts import render
from rest_framework import status
from rest_framework.generics import GenericAPIView, ListAPIView

from django_redis import get_redis_connection
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.views import APIView

from goods.models import SKU
from orders.models import OrderInfo, OrderGoods
from orders.serializers import OrderPlaceSerializer, OrderCommitSerializer, OrderCommentSerializer, \
    CommentGoodsSerializer
from decimal import Decimal
from rest_framework.generics import CreateAPIView



class OrderPlaceView(GenericAPIView):
    serializer_class = OrderPlaceSerializer
    permission_classes = [IsAuthenticated]

    def get(self, request):
        # 1,获取redis中需要结算的商品
        user = request.user
        redis_conn = get_redis_connection("cart")
        cart_dict = redis_conn.hgetall("cart_%s" % user.id)
        cart_selected_list = redis_conn.smembers("cart_selected_%s"%user.id)

        # 2,拼接数据
        sku_list = []
        for sku_id  in cart_selected_list:
            sku = SKU.objects.get(id=sku_id)
            sku.count = int(cart_dict[sku_id])
            sku_list.append(sku)

        # 3,获取序列化器
        serializer = self.get_serializer(instance=sku_list,many=True)

        # 4,返回响应
        contenxt = {
            "skus":serializer.data,
            "freight":Decimal(10.0)
        }
        return Response(contenxt)

class OrderCommitView(CreateAPIView):
    serializer_class = OrderCommitSerializer
    permission_classes = [IsAuthenticated]

#获取评论信息
class CommentGoodsView(ListAPIView):
    serializer_class = CommentGoodsSerializer
    def get_queryset(self):
        query=OrderGoods.objects.filter(sku_id=self.kwargs.get('sku_id'),is_commented=True)
        # user=OrderInfo.objects.filter(order_id=self.kwargs.get('order_id'),FINISHED=5)
        for ordergood in query:
            ordergood.username= ordergood.order.user.username

        return query


class OrderCommentView(GenericAPIView):
    serializer_class =OrderCommentSerializer
    permission_classes = [IsAuthenticated]
    #展示待评论
    def get(self,request,order_id):

        queryset=OrderGoods.objects.filter(order_id=order_id,is_commented = False).all()

        serializer=self.get_serializer(instance=queryset,many=True)

        return Response(serializer.data,status=status.HTTP_200_OK)
    #保存上传评论
    def post(self,request,order_id):

        #获取数据
        dict_data=request.data
        #获取序列化器，校验数据
        serializer=self.get_serializer(data=dict_data)
        serializer.is_valid(raise_exception=True)

        #数据入库
        serializer.save()
        #返回
        return Response(serializer.data)




# class OrderCommentView(GenericAPIView):
#     serializer_class = OrderCommentSerializer
#
#     def get(self,request):
#         order_id=request.query_params.get("order_id")
#         id=OrderGoods.objects.filter(order_id=order_id)
#         sku=SKU.objects.filter(sku_id=id)
#         return sku